﻿using System;
using System.Configuration;
using WxTenpay.Common;

namespace WxTenpay
{
    /// <summary>
    /// 获取配置文件配置
    /// </summary>
    public static class GetConfig
    {



        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetIndexConfigValue(string key)
        {
            try
            {
                string indexConfigPath = "WxTenpay.config";

                ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
                ecf.ExeConfigFilename = System.AppDomain.CurrentDomain.BaseDirectory + indexConfigPath;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
                return config.AppSettings.Settings[key].Value;
            }
            catch (Exception ex)
            { //没有文件，读取异常不做处理
                return null;
            }
        }

        /// <summary>
        /// 重置配置文件
        /// </summary>
        /// <returns></returns>
        public static void ResetConfig()
        {
            //  var model = ConfigExtensions.GetKeyJson(path, key);



            string appid = ConfigExtensions.GetKeyJson()["wxconfig:AppID"];// GetIndexConfigValue("AppID");
            string Secret = ConfigExtensions.GetKeyJson()["wxconfig:Secret"];
            string MchId = ConfigExtensions.GetKeyJson()["wxconfig:MchId"];
            string TenPayKey = ConfigExtensions.GetKeyJson()["wxconfig:TenPayKey"];
            string URL = ConfigExtensions.GetKeyJson()["wxconfig:URL"];

            string SSLCERTPATH = ConfigExtensions.GetKeyJson()["wxconfig:SSLCERTPATH"];
            string SSLCERTPASSWORD = ConfigExtensions.GetKeyJson()["wxconfig:SSLCERTPASSWORD"];
            string AILACCESSKEYID =  ConfigExtensions.GetKeyJson()["wxconfig:AILACCESSKEYID"];
            string AILACCESSKEYSECRET = ConfigExtensions.GetKeyJson()["wxconfig:AILACCESSKEYSECRET"];


            string BAIDUMAPAK = ConfigExtensions.GetKeyJson()["wxconfig:BAIDUMAPAK"];
            string AAppID = ConfigExtensions.GetKeyJson()["wxconfig:AAppID"];
            string AMchId = ConfigExtensions.GetKeyJson()["wxconfig:AMchId"];
            string ATenPayKey = ConfigExtensions.GetKeyJson()["wxconfig:ATenPayKey"];
            string AURL = ConfigExtensions.GetKeyJson()["wxconfig:AURL"];
            string IAppID = ConfigExtensions.GetKeyJson()["wxconfig:IAppID"];
            string IMchId = ConfigExtensions.GetKeyJson()["wxconfig:IMchId"];
            string ITenPayKey = ConfigExtensions.GetKeyJson()["wxconfig:ITenPayKey"];
            string IURL = ConfigExtensions.GetKeyJson()["wxconfig:IURL"];

            string Token = ConfigExtensions.GetKeyJson()["wxconfig:Token"];



            if (!string.IsNullOrWhiteSpace(appid))
                WXconfig.appid = appid;
            if (!string.IsNullOrWhiteSpace(Secret))
                WXconfig.secret = Secret;

            if (!string.IsNullOrWhiteSpace(MchId))
                WXconfig.mch_id = MchId;
            if (!string.IsNullOrWhiteSpace(TenPayKey))
                WXconfig.paysignkey = TenPayKey;

            if (!string.IsNullOrWhiteSpace(SSLCERTPATH))
                WXconfig.SSLCERT_PATH = SSLCERTPATH;

            if (!string.IsNullOrWhiteSpace(SSLCERTPASSWORD))
                WXconfig.SSLCERT_PASSWORD = SSLCERTPASSWORD;

            if (!string.IsNullOrWhiteSpace(URL))
                WXconfig.url = URL;




            if (!string.IsNullOrWhiteSpace(Token))
                WXconfig.Token = Token;
            if (!string.IsNullOrWhiteSpace(IAppID))
                APP_Iconfig.appid = AAppID;
            if (!string.IsNullOrWhiteSpace(IURL))
                APP_Iconfig.url = IURL;
            if (!string.IsNullOrWhiteSpace(IMchId))
                APP_Iconfig.mch_id = IMchId;
            if (!string.IsNullOrWhiteSpace(ITenPayKey))
                APP_Iconfig.paysignkey = ITenPayKey;


            if (!string.IsNullOrWhiteSpace(AAppID))
                APP_Aconfig.appid = AAppID;
            if (!string.IsNullOrWhiteSpace(AURL))
                APP_Aconfig.url = AURL;
            if (!string.IsNullOrWhiteSpace(AMchId))
                APP_Aconfig.mch_id = AMchId;
            if (!string.IsNullOrWhiteSpace(ATenPayKey))
                APP_Aconfig.paysignkey = ATenPayKey;



        }

    }
}
